package com.lvmama.pangolin.controller;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageInfo;
import com.lvmama.pangolin.busi.timer.TimerManagerAssist;
import com.lvmama.pangolin.common.StringTools;
import com.lvmama.pangolin.entity.JobConfig;
import com.lvmama.pangolin.entity.JobListConditionVO;
import com.lvmama.pangolin.entity.Timer;
import com.lvmama.pangolin.entity.TimerListConditionVO;
import com.lvmama.pangolin.enums.CommonStringType;
import com.lvmama.pangolin.service.config.BackJobConfigService;

@Controller
@RequestMapping("backJobConfig")
@PreAuthorize("hasAnyRole('admin')")
public class JobConfigController {

	private static Logger log = Logger.getLogger(JobConfigController.class);
	
	@Autowired 
	private BackJobConfigService jobConfigService;

    @RequestMapping("timerList")
    public String timerList(ModelMap model,TimerListConditionVO timerListConditionVO) throws Exception{
    	 CommonStringType[]  timerStatusTypeList = CommonStringType.getTimerStatusType();
    	 PageInfo<Timer> page =jobConfigService.getTimerList(timerListConditionVO);
    	 if(!StringTools.isValid(timerListConditionVO.getTimerStatus()))
    	 {
    		 timerListConditionVO.setTimerStatus("0");
    	 }
         List<Timer> timerVOList = page.getList();
         model.addAttribute("page", page);
         model.addAttribute("timerVOList",timerVOList);
         model.addAttribute("timerStatusTypeList",timerStatusTypeList);
         model.addAttribute("configListConditionVO",timerListConditionVO);
         return "background/config/timerList";
    }
    
 
	@RequestMapping(value = "/TimerEdit", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
    public Map<String, Object> configEdit(ModelMap model ,Timer timer){
    	Map<String, Object> map = new HashMap<String, Object>(1);
    	int code = 1;
    	List<Timer> timerVOList;
		try {
			 timerVOList = jobConfigService.getTimerByIds(timer);
			 if(null != timerVOList && timerVOList.size()>0){
	    	     timer =timerVOList.get(0); 
	    	 }
	        
		}catch (Exception e) {
			log.info("JobConfigController...TimerEdit memthod error");
			code = 0;
			e.printStackTrace();
		}
		map.put("code",code);
		map.put("timerVO",timer);
       
		return map;
       
    }
	
	@RequestMapping(value = "/TimerRefresh", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
    public Map<String, Object> timerRefresh(ModelMap model ,Timer timer){
    	Map<String, Object> map = new HashMap<String, Object>(1);
    	int code = 0;
    	List<Timer> timerVOList;
		try {
			 timerVOList = jobConfigService.getTimerByIds(timer);
			 if(null != timerVOList && timerVOList.size()>0){
	    	     timer =timerVOList.get(0); 
	    	     TimerManagerAssist timerAssist = new TimerManagerAssist();
	    	     timerAssist.refreshJob(timer);
	    	 }
	        
		}catch (Exception e) {
			log.info("JobConfigController...TimerRefresh memthod error");
			code = 1;
			e.printStackTrace();
			System.out.println("111111111111");
		}
		map.put("code",code);
		map.put("timerVO",timer);
       
		return map;
       
    }
	
	@RequestMapping(value = "/TimerFire", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
    public Map<String, Object> timerFire(ModelMap model ,Timer timer){
    	Map<String, Object> map = new HashMap<String, Object>(1);
    	int code = 0;
    	List<Timer> timerVOList;
		try {
			 timerVOList = jobConfigService.getTimerByIds(timer);
			 if(null != timerVOList && timerVOList.size()>0){
	    	     timer =timerVOList.get(0); 
	    	     TimerManagerAssist timerAssist = new TimerManagerAssist();
	    	     String timerCode = timer.getTimer_code()+"_"+timer.getTask_id();
	    	     timerAssist.fireJob(timerCode);
	    	 }
	        
		}catch (Exception e) {
			log.info("JobConfigController...TimerFire memthod error");
			code = 1;
		}
		map.put("code",code);
		map.put("timerVO",timer);
       
		return map;
       
    }
	

    
    @RequestMapping("timerUpdate")
    public String configAdd(ModelMap model ,Timer timer){
    	List<Timer> timerVOList = new ArrayList<Timer>(); 
    	TimerListConditionVO timerListConditionVO = new TimerListConditionVO();
    	CommonStringType[]  timerStatusTypeList = CommonStringType.getTimerStatusType();
    	PageInfo<Timer> page = new PageInfo<Timer>();
    	Date nowDate = new Date();
    	try {
    		 if(null != timer.getTimer_id() && timer.getTimer_id()!=""){
    			 timer.setOper_time(nowDate);
    			 jobConfigService.updateTimer(timer);
    		 }
	    	 page = jobConfigService.getTimerList(timerListConditionVO);
	    	 timerVOList = page.getList();
		} catch (Exception e) {
			log.info("JobConfigController...timerUpdate memthod error");
			e.printStackTrace();
		}
    	 model.addAttribute("page", page);
         model.addAttribute("timerVOList",timerVOList);
         model.addAttribute("timerListConditionVO",timerListConditionVO);
         model.addAttribute("timerStatusTypeList",timerStatusTypeList);
         return "background/config/timerList";
    }
    
    @RequestMapping("jobList")
    public String jobList(ModelMap model,JobListConditionVO jobListConditionVO) throws Exception{
    	 PageInfo<JobConfig> page =jobConfigService.getJobList(jobListConditionVO);
         List<JobConfig> jobVOList = page.getList();
         model.addAttribute("page", page);
         model.addAttribute("jobVOList",jobVOList);
         model.addAttribute("jobListConditionVO",jobListConditionVO);
    	 return "background/config/jobList";
    }
    

    @RequestMapping(value ="/showDetails",method = { RequestMethod.GET,RequestMethod.POST })
	@ResponseBody
    public Map<String, Object> showDetails(ModelMap model , JobConfig jobConfig){
    	Map<String, Object> map = new HashMap<String ,Object>();
    	List<JobConfig> jobChildsList;
		try {
			jobChildsList = jobConfigService.getJobChildsList(jobConfig);
			map.put("jobChildsList", jobChildsList);
			map.put("code", 1);
		} catch (Exception e) {
			map.put("code", 0);
			e.printStackTrace();
			log.info("JobConfigController...showDetails memthod error");
		}
        
    	 return map;
    }
    
    @RequestMapping("jobUpdate")
    public String jobUpdate(JobConfig jobConfig,ModelMap model){
    	List<JobConfig> jobVOList = new ArrayList<JobConfig>(); 
    	JobListConditionVO jobListConditionVO = new JobListConditionVO();
    	PageInfo<JobConfig> page = new PageInfo<JobConfig>();
    	Date nowDate = new Date();
    	try {
    		 if(null != jobConfig.getJobconfig_id()){
    			 jobConfig.setOper_time(nowDate);
    			 jobConfigService.updateJob(jobConfig);
    		 }else{
    			 jobConfig.setCreate_time(nowDate);
    			 jobConfigService.addJob(jobConfig); 
    		 }
	    	 page = jobConfigService.getJobList(jobListConditionVO);
	    	 jobVOList = page.getList();
		} catch (Exception e) {
			e.printStackTrace();
			log.info("JobConfigController...jobUpdate memthod error");
		}
    	 model.addAttribute("page", page);
         model.addAttribute("jobVOList",jobVOList);
         model.addAttribute("jobListConditionVO",jobListConditionVO);
    	 return "background/config/jobList";
    }
   
	@RequestMapping(value = "/jobEdit", method = { RequestMethod.GET,
			RequestMethod.POST })
	@ResponseBody
    public Map<String, Object> jobEdit(ModelMap model ,JobConfig jobConfig){
    	Map<String, Object> map = new HashMap<String, Object>(1);
    	int code = 1;
    	List<JobConfig> configVOList;
		try {
			configVOList = jobConfigService.getJobByIds(jobConfig);
			 if(null != configVOList && configVOList.size()>0){
				 jobConfig =configVOList.get(0); 
	    	 }
	        
		} catch (Exception e) {
			code = 0;
			e.printStackTrace();
			log.info("JobConfigController...jobEdit memthod error");
		}
		map.put("code",code);
		map.put("jobConfigVO",jobConfig);
		
		return map;
       
    }
    
	
	  @RequestMapping("jobConfigDelete")
	  @ResponseBody
	  public Map<String, Object> jobDelete(ModelMap model ,JobConfig jobConfig) throws Exception{
		      Map<String, Object> map = new HashMap<String,Object>();
		      try{
		    	  jobConfigService.deleteJob(jobConfig);
		    	  map.put("code", "1");
		      }catch(Exception e){
		    	  map.put("code", "0");
		    	  log.info("BackGroundController ... method configDelete error" + e); 
		      }
		      JobListConditionVO jobListConditionVO = new JobListConditionVO();
	     	  PageInfo<JobConfig> page = jobConfigService.getJobList(jobListConditionVO);
	          List<JobConfig> jobVOList = page.getList();
	          model.addAttribute("page", page);
	          model.addAttribute("jobVOList",jobVOList);
	          model.addAttribute("jobListConditionVO",jobListConditionVO);
	          return map;
	    }

}
